/*
 * Created by JFormDesigner on Wed Dec 15 14:35:25 CST 2021
 */

package com.cn.项目.飞机预约管理系统.Maininterface;

import com.cn.项目.utils.ImageUtil;
import com.cn.项目.utils.JdbcUtils;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;


public class AddPassenger01 extends JPanel {
    public AddPassenger01() {
        initComponents();
    }

    String path = null;
    //增加监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        String pname = textField1.getText();
        String pcard = textField2.getText();
        FileInputStream in = null;
        String dname = textField4.getText();

        try {
            in = ImageUtil.readImage(path);
            conn  = JdbcUtils.getConnection();
            String sql = "insert into passenger(pname,pcard,photo,dname)values (?,?,?,?)";
            st  = conn.prepareStatement(sql);
            st.setString(1, pname);
            st.setString(2, pcard);
            st.setBinaryStream(3, in, in.available());
            st.setString(4,dname);
            int i = st.executeUpdate();
            if(i>0){
                new Success();
            }else {
                new Fail();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st ,rs);
        }

    }


    //重置
    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        textField1.setText(" ");
        textField2.setText(" ");
        textField4.setText(" ");
    }

    //添加照片
    private void button3ActionPerformed(ActionEvent e) {
        // TODO add your code here
        path = showFileOpenDialog(this);
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        label3 = new JLabel();
        textField2 = new JTextField();
        label5 = new JLabel();
        textField4 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("增加乘客");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 16f));
        label1.setForeground(new Color(204, 0, 51));
        add(label1);
        label1.setBounds(125, 5, 245, 55);

        //---- label2 ----
        label2.setText("乘客姓名");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        add(label2);
        label2.setBounds(25, 75, 100, 40);
        add(textField1);
        textField1.setBounds(135, 75, 180, 35);

        //---- label3 ----
        label3.setText("身份证：");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        add(label3);
        label3.setBounds(25, 145, 75, 35);
        add(textField2);
        textField2.setBounds(135, 145, 180, 35);

        //---- label5 ----
        label5.setText("代买人");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 6f));
        add(label5);
        label5.setBounds(30, 275, 75, 30);
        add(textField4);
        textField4.setBounds(135, 270, 180, 40);

        //---- button1 ----
        button1.setText("增加");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
        button1.setForeground(Color.green);
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1);
        button1.setBounds(55, 340, 105, 35);

        //---- button2 ----
        button2.setText("重置");
        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 6f));
        button2.setForeground(Color.red);
        button2.addActionListener(e -> button2ActionPerformed(e));
        add(button2);
        button2.setBounds(230, 335, 110, 40);

        //---- button3 ----
        button3.setText("添加照片");
        button3.setFont(button3.getFont().deriveFont(button3.getFont().getSize() + 6f));
        button3.setForeground(new Color(0, 204, 102));
        button3.addActionListener(e -> button3ActionPerformed(e));
        add(button3);
        button3.setBounds(new Rectangle(new Point(20, 215), button3.getPreferredSize()));

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JLabel label3;
    private JTextField textField2;
    private JLabel label5;
    private JTextField textField4;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    String showFileOpenDialog(AddPassenger01 parent) {
        // 创建一个默认的文件选择器
        JFileChooser fileChooser = new JFileChooser();
        // 设置默认显示的文件夹
        fileChooser.setCurrentDirectory(new File("D:\\"));
        // 添加可用的文件过滤器（FileNameExtensionFilter 的第一个参数是描述, 后面是需要过滤的文件扩展名）
//        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("(txt)", "txt"));
        // 设置默认使用的文件过滤器（FileNameExtensionFilter 的第一个参数是描述, 后面是需要过滤的文件扩展名 可变参数）
        fileChooser.setFileFilter(new FileNameExtensionFilter("(jpg)", "jpg"));
        // 打开文件选择框（线程将被堵塞，知道选择框被关闭）
        int result = fileChooser.showOpenDialog(parent);  // 对话框将会尽量显示在靠近 parent 的中心

        String path = null;
        // 点击确定
        if(result == JFileChooser.APPROVE_OPTION) {
            // 获取路径
            File file = fileChooser.getSelectedFile();
            path = file.getAbsolutePath();
        }
        return path;
    }
}
